<template>
	<view class="content">
		<view style="height: 20rpx;"></view>
		<view style="background-color: #fff;width: calc(100% - 60rpx);margin: 0 auto 20rpx;padding: 20rpx 15rpx;">
			<!-- <button type="primary" style="width: 200rpx;height: 50rpx;line-height: 50rpx;font-size: 22rpx;
			background-color: #5E9CF1;margin: 20rpx 40rpx;" @click="showPicker">添加子点位</button> -->
			<view style="font-size: 22rpx; padding: 0 15rpx;">
				<view v-if="parentUnit" style="width: 400rpx;display: flex; align-items: center;">
					<view style="margin-right: 10rpx; width: 90rpx;">父单位</view>
					<uni-easyinput placeholder="父单位" v-model="parentUnit" :clearable="false"></uni-easyinput>
				</view>
				<view style="width: 400rpx;display: flex; align-items: center; margin-top: 20rpx;">
					<view style="margin-right: 10rpx; width: 90rpx;">所属单位</view>
					<uni-easyinput placeholder="所属单位" v-model="sysCompanyFindOne.name" :clearable="false"></uni-easyinput>
				</view>
				<view style="width: 400rpx;display: flex; align-items: center; margin-top: 20rpx;">
					<view style="margin-right: 10rpx; width: 90rpx; text-align: right;">备注</view>
					<uni-easyinput placeholder="备注" v-model="sysCompanyFindOne.note" :clearable="false"></uni-easyinput>
				</view>
				<!-- <view style="margin-left: 100rpx; margin-top: 20rpx; overflow: hidden;">
					<button type="primary" style="width: 100rpx;height: 50rpx;line-height: 50rpx;
					font-size: 22rpx;background-color: #5E9CF1; float: left;" 
					@click="save">{{!parentUnit && unitId ?'修改':'保存'}}  </button>
					<button v-if="!parentUnit && unitId" type="primary" style="width: 100rpx;height: 50rpx;line-height: 50rpx; margin-left: 20rpx;
					font-size: 22rpx;background-color: red; float: left;" @click="deleteTo"> 删除</button>
				</view> -->
			</view>
			<ba-tree-picker-one ref="treePickerone" :multiple='false' @select-change="selectChange" title="所属单位"
				:localdata="listData" valueKey="id" textKey="name" childrenKey="children" :confirmColor="'#0055ff'" />
		</view>
	
	</view>
</template>

<script>
	// import baTreePicker from "@/components/ba-tree-picker/ba-tree-picker.vue";
	import baTreePickerOne from "@/components/ba-tree-picker/ba-tree-picker-one.vue";
	export default {
		components: {
			// baTreePicker,
			baTreePickerOne
		},
		data() {
			return {
				listData: [],
				sysCompanyFindOne:null,
				// parentUnitId:null,
				// parentUnit:'',
				// unitId:'',
				// unit:'',
				// notes:'',
				// type:'',
				// ids:'',
				// names:'',
			}
		},
		methods: {
			// data 是树型数组
			// key 是String 对象中的key值
			// value 是key值符合的条件
			// type 删除还是添加
			// getAllChidlren(data, key, value, type) {
			// 	var that = this
			//   var result = [];
			//   var fn = function(data) {
			//     if (Array.isArray(data)) { // 判断是否是数组并且没有的情况下，
			//       data.forEach((item,index) => {
			//         if (item[key] === value) { // 数据循环每个子项
			//           result.push(item); // 返回的结果等于每一项
			// 		  if(type == 'add'){
			// 			  if(item.children){
			// 			  	item.children.push({
			// 			  		id: item.children[item.children.length - 1].id + 1,
			// 			  		name: that.unit,
			// 			  	})
			// 			  }else{
			// 			  	item.children = [{
			// 			  		id: item.id + '1',
			// 			  		name: that.unit,
			// 			  	}]
			// 			  }
			// 			  uni.showToast({
			// 			  	title: '保存成功',
			// 			  	icon: 'none'
			// 			  });
			// 		  }
			// 		  if(type == 'delete'){
			// 			  data.splice(index,1)
			// 			  uni.showToast({
			// 			  	title: '删除成功',
			// 			  	icon: 'none'
			// 			  });
			// 		  }
			// 		  if(type == 'updata'){
			// 			  item.name = that.unit
			// 			  uni.showToast({
			// 			  	title: '修改成功',
			// 			  	icon: 'none'
			// 			  });
			// 		  }
			// 		  that.parentUnitId = ''
			// 		  that.parentUnit = ''
			// 		  that.notes = ''
			// 		  that.unitId = ''
			// 		  that.unit = ''
			//         } else if (item.children) {
			//           fn(item.children); // 递归调用下边的子项
			//         }
			//       })
			//     }
			//   }
			//   fn(data); // 调用一下
			//   return result;
			// },
			// save(){
			// 	if(!this.parentUnit && !this.unitId) return uni.showToast({
			// 		title: '请先选择父单位',
			// 		icon: 'none'
			// 	});
			// 	if(this.parentUnit == '' && this.type == 'add') return uni.showToast({
			// 		title: '请先选择父单位',
			// 		icon: 'none'
			// 	});
			// 	if(this.unit == '') return uni.showToast({
			// 		title: '请填写所属单位',
			// 		icon: 'none'
			// 	});
			// 	if(this.type == 'add'){
			// 		this.getAllChidlren(this.listData,'id',this.ids,'add')
			// 	}
			// 	if(this.type == 'updata'){
			// 		this.getAllChidlren(this.listData,'id',this.ids,'updata')
			// 	}
			// },
			// deleteTo(){
			// 	if(this.unit == '') return uni.showToast({
			// 		title: '请先选择单位',
			// 		icon: 'none'
			// 	});
			// 	this.getAllChidlren(this.listData,'id',this.ids,'delete')
			// 	console.log(this.getAllChidlren(this.listData,'id',this.ids,'delete'))
			// },
			// showPicker() {
			// 	if(this.unitId == '') return uni.showToast({
			// 		title: '请先选择父单位',
			// 		icon: 'none'
			// 	});
			// 	this.type = 'add'
			// 	this.parentUnitId = this.ids
			// 	this.parentUnit = this.names
			// 	this.unitId = ''
			// 	this.unit = ''
			// },
			//监听选择（ids为数组）
			selectChange(ids, names) {
				uni.showLoading({title: '数据加载中...',mask: true})
				this.$http.get('/api/v1/sysCompany/findOne',{id:ids[0]}, true).then(res => {
					if (res.data.item) {
						this.sysCompanyFindOne = res.data.item
					} else {
						uni.showToast({title: res.msg,icon: 'none'});
					}
				}).catch(err => {
					uni.showToast({title: err.msg,icon: 'none'});
				})
				console.log(ids, names)
				// this.type = 'updata'
				// this.parentUnit = ''
				// this.notes = ''
				// this.unitId = ids[0]
				// this.unit = names
				// this.ids = ids[0]
				// this.names = names
			}
			
			
		},
		onLoad(e) { //页面加载完执行
			this.$refs.treePickerone._show();
			if(this.$method.getStorageTo("findTree")){
				this.listData = this.$method.getStorageTo("findTree")
			}else{
				uni.showLoading({title: '数据加载中...',mask: true})
				this.$http.get('/api/v1/sysCompany/findTree',{}, true).then(res => {
					if (res.data.items) {
						this.listData = res.data.items
						this.$method.setStorageTo("findTree", res.data.items)
					} else {
						uni.showToast({title: res.msg,icon: 'none'});
					}
				}).catch(err => {
					uni.showToast({title: err.msg,icon: 'none'});
				})
			}
		},
		onShow() { //每次进入页面执行

		}
	}
</script>

<style lang="scss">
	// $color-too:#7468BE;
	page {
		width: 100%;
		height: 100%;
		margin: 0;
		padding: 0;
	}

	// .uni-select{
	// 		height: 50rpx !important;
	// 	}
	// 	.uni-easyinput__content-input {
	// 		height: 50rpx !important;
	// 	}
	// 	.content {
	// 		font-size: 14px;
	// 		// height: 100%;
	// 		overflow-y: scroll;
	// 	}
</style>